package com.sixmac.platform.intercept;

import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.sixmac.core.aop.AopContext;
import com.sixmac.core.meta.PageIntercept;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.toolbox.grid.BladePage;
import com.sixmac.core.toolbox.support.Convert;
import com.sixmac.model.Category;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserIntercept extends PageIntercept {

    /**
     * 查询后操作 字典项、部门不通过数据库查询,通过缓存附加,减轻数据库压力,提高分页效率
     *
     * @param ac
     */
    @SuppressWarnings("unchecked")
    public void queryAfter(AopContext ac) {
        BladePage<Map<String, Object>> page = (BladePage<Map<String, Object>>) ac.getObject();
        List<Map<String, Object>> list = page.getRows();
        for (Map<String, Object> map : list) {
            if (!Strings.isNullOrEmpty(Convert.toStr(map.get("cids")))) {
                Integer[] ids = Convert.toIntArray(Convert.toStr(map.get("cids")));
                List<String> names = new ArrayList<>();
                for (Integer id : ids) {
                    Category category = Blade.create(Category.class).findById(id);
                    names.add(category.getName());
                }
                map.put("cname", Joiner.on(",").join(names));
            } else {
                map.put("cname", "暂无分类");
            }
        }
    }
}
